import config from "@/config";

export default {
  // 检查更新
  checkUpdate() {
    // #ifdef APP-PLUS
    return new Promise((resolve, reject) => {
      plus.runtime.getProperty(plus.runtime.appid, inf => {
        let formData = Object.assign({
          mode: process.env.NODE_ENV === 'development' ? 1 : 0,
          platform: plus.os.name.toLowerCase() === 'android' ? 'android' : 'ios',
        }, inf);
        uni.request({
          url: config.CHECK_UPDATE_URL,
          data: formData,
          success: result => {
            resolve(result && result.data.data && result.data.data.update ? result.data.data : false);
          },
          fail: (err) => {
            reject(err);
          }
        });
      });
    });
    // #endif
    // #ifndef APP-PLUS
    return Promise.reject(new Error("仅app端支持在线更新"))
    // #endif
  },
  /**
   * @param {Object} url  下载地址
   * @param {Object} progress 进度信息
   */
  downloadFile(url, progress) {
    console.warn({
      url
    })
    const path = uni.getStorageSync("updateInfo_" + url);
    if (path) {}
    return new Promise((resolve, reject) => {
      // 开始下载
      let downloadTask = uni.downloadFile({
        url,
        success: (result) => {
          resolve(result);
        },
        fail: (err) => {
          reject(err);
        }
      });
      downloadTask.onProgressUpdate(function(res) {
        typeof progress === "function" && progress(res);
      });
    });











  }
};
